/**
 * <pre>
 * Copyright Notice:
 *    Copyright (c) 2005-2009 China Financial Certification Authority(CFCA)
 *    A-1 You An Men Nei Xin An Nan Li, Xuanwu District, Beijing ,100054, China
 *    All rights reserved.
 * 
 *    This software is the confidential and proprietary information of
 *    China Financial Certification Authority (&quot;Confidential Information&quot;).
 *    You shall not disclose such Confidential Information and shall use 
 *    it only in accordance with the terms of the license agreement you 
 *    entered into with CFCA.
 * </pre>
 */

package payment.simulator.institution.tx.aggregate;

import payment.api.system.AggregateEnvironment;
import payment.api.tx.aggregate.Tx5001Request;
import payment.api.util.GUIDGenerator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

public class AutoTx5001 extends HttpServlet {

    private static final long serialVersionUID = -7230771760626665867L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.取得参数
            String notificationURL = null;
            if (request.getHeader("Referer") == null) {
                notificationURL = request.getRequestURL().substring(0,
                        request.getRequestURL().indexOf(request.getRequestURI()) + request.getContextPath().length())
                        + "/ReceiveNoticePage";
            } else {
                notificationURL = request.getHeader("Referer").substring(0,
                        request.getHeader("Referer").indexOf(request.getContextPath()) + request.getContextPath().length())
                        + "/ReceiveNoticePage";
            }
            String remark = "";

            // 2.创建交易请求对象
            Tx5001Request tx5001Request = new Tx5001Request();
            tx5001Request.setInstitutionID("000020");
            tx5001Request.setOrderNo(GUIDGenerator.genGUID());
            tx5001Request.setMerchantName("测试");
            tx5001Request.setUserID("0001");
            tx5001Request.setAmount("1");
            tx5001Request.setCreditFlag("10");
            tx5001Request.setPaymentMode("10");
            tx5001Request.setPaymentWay("10");
            tx5001Request.setSettlementFlag("0001");
            tx5001Request.setGoodsName("0001");
            tx5001Request.setPaymentScene("10");
            tx5001Request.setExpirePeriod("");
            tx5001Request.setNotificationURL(notificationURL);
            tx5001Request.setRemark(remark);

            // 3.执行报文处理
            tx5001Request.process();

            // 4.将参数放置到request对象
            // //2个交易参数
            request.setAttribute("message", tx5001Request.getRequestMessage());
            request.setAttribute("signature", tx5001Request.getRequestSignature());
            // //2个信息参数
            request.setAttribute("txCode", "5001");
            request.setAttribute("txName", "统一下单");
            // 1个action(支付平台地址)参数
            request.setAttribute("action", AggregateEnvironment.aggregatePaymentURL);
            if (StringUtils.isEmpty(AggregateEnvironment.aggregatePaymentURL)) {
                processException(request, response, "未配置gateway4aggregate.payment.url");
            }
            // 5.转向Request.jsp页面
            request.getRequestDispatcher("/AutoRequest.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            processException(request, response, e.getMessage());
        }
    }

    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
        try {
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        } catch (Exception e) {
        }
    }

}
